package com.example.travel.controller;

import com.example.travel.pojo.RouteImg;
import com.example.travel.service.RouteImgService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("admin/route")
public class RouteImgController {

    @Autowired
    private RouteImgService routeImgService;

    //获取到图片信息传递个img页面
    @RequestMapping("imgList/{rid}")
    public String getImgList(@PathVariable("rid") Integer rid,
                             Model model){

        List<RouteImg> routeImgList = routeImgService.getRouteImgByRid(rid);

        model.addAttribute("rid",rid);
        model.addAttribute("routeImgList",routeImgList);

        return "route/image";
    }

    //保存图片信息
    @RequestMapping("saveImg")
    public String addImg(Integer rid,
                          @RequestParam("bigPicFile") MultipartFile[] bigPicFile,
                          @RequestParam("smallPicFile") MultipartFile[] smallPicFile,
                          HttpServletRequest request) throws IOException {
        List<String> bigPic = new ArrayList<>();
        List<String> smallPic = new ArrayList<>();

        //String path = request.getServletContext().getRealPath("/");
        String path = "C:/Users/srq/IdeaProjects/travel/src/main/resources/static/";

        for(MultipartFile f : bigPicFile) {
            File bigPath = new File(path+"img/product/bigPic/");
            if (!bigPath.exists()){
                bigPath.mkdirs();
            }

            String fileName = UUID.randomUUID()
                    .toString().replaceAll("-","")+"."+
                    FilenameUtils.getExtension(f.getOriginalFilename());
            f.transferTo(new File(bigPath,fileName));
            bigPic.add("img/product/bigPic/"+fileName);

        }

        for(MultipartFile f : smallPicFile) {
            File smallPath = new File(path+"img/product/smallPic/");
            if (!smallPath.exists()){
                smallPath.mkdirs();
            }

            String fileName = UUID.randomUUID()
                    .toString().replaceAll("-","")+"."+
                    FilenameUtils.getExtension(f.getOriginalFilename());
            f.transferTo(new File(smallPath,fileName));
            smallPic.add("img/product/smallPic/"+fileName);
        }

        //要添加的图片列表
        ArrayList<RouteImg> rImgList = new ArrayList<>();

        for(int i=0;i<bigPic.size();i++){
            RouteImg img = new RouteImg();
            img.setRid(rid);
            img.setBigPic(bigPic.get(i));
            img.setSmallPic(smallPic.get(i));

            rImgList.add(img);

        }

        routeImgService.updateRouteImg(rid,rImgList);

        return "redirect:/admin/route/page";
    }
}
